package com.ruoyi.web.controller.system;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.event.Order;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.http.HmdBo;
import com.ruoyi.common.utils.http.HttpDomain;
import com.ruoyi.common.utils.http.SendHttpPost;
import com.ruoyi.common.utils.sign.RSAEncryptionDecryption;
import com.ruoyi.system.domain.SysTenant;
import com.ruoyi.system.domain.TzCollectionOrder;
import com.ruoyi.system.domain.vo.ApiParam;
import com.ruoyi.system.service.ISysTenantService;
import com.ruoyi.system.service.TzCollectionOrderService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/business/api")
@Api(tags = "催款订单管理")
public class TzApiCollectionOrderController extends BaseController {


    @Autowired
    private TzCollectionOrderService tzCollectionOrderService;
    @Autowired
    private ISysTenantService sysTenantService;


    @PostMapping("/pushOrder")
    @ApiOperation(value = "催款订单推送", notes = "催款订单推送")
    public AjaxResult pushOrder(@RequestBody ApiParam apiParam) {
        SysTenant tenant = sysTenantService.lambdaQuery().eq(SysTenant::getTenantId, apiParam.getAppId()).one();
        if (ObjectUtil.isEmpty(tenant)){
            return AjaxResult.error("租户不存在");
        }
        String param="";
        try {
            param=RSAEncryptionDecryption.decrypt(apiParam.getSign(),tenant.getAppPublicKey());
        }catch (Exception e){
            return AjaxResult.error("签名错误");
        }
        List<TzCollectionOrder> orders=new ArrayList<>();
        try {
            System.err.println(param);
            orders = JSONObject.parseObject(param, List.class);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("参数错误");
        }
        HttpDomain domain = new HttpDomain();

        List<HmdBo> list=new ArrayList<>();
        for (TzCollectionOrder order : orders) {
            order.setTenantId(tenant.getId().toString());
            if (ObjectUtil.isNotEmpty(tzCollectionOrderService.lambdaQuery().eq(TzCollectionOrder::getOutOrderNo, order.getOutOrderNo()).one())) {
                return AjaxResult.error("订单已存在");
            }
            HmdBo bo=new HmdBo();
            bo.setRealName(order.getCustomerName());
            bo.setIdCard(order.getIdCard());
            bo.setMobile(order.getMobile());
            bo.setRemark(order.getRemark());
            bo.setType(order.getType());
            tzCollectionOrderService.save(order);
            list.add(bo);
        }
        SendHttpPost.sendHttpPost(domain);
        return AjaxResult.success();
    }


    @PostMapping("/getOrderStatus/{outOrderNo}")
    @ApiOperation(value = "催款订单状态查询", notes = "催款订单状态查询")
    public AjaxResult getOrderStatus(@PathVariable String outOrderNo,@RequestBody ApiParam apiParam) {
        SysTenant tenant = sysTenantService.lambdaQuery().eq(SysTenant::getTenantId, apiParam.getAppId()).one();

        if (ObjectUtil.isEmpty(tenant)){
            return AjaxResult.error("租户不存在");
        }
        String param="";
        TzCollectionOrder one = tzCollectionOrderService.lambdaQuery().eq(TzCollectionOrder::getOutOrderNo, outOrderNo)
                .eq(TzCollectionOrder::getTenantId, tenant.getId().toString()).one();
        if (ObjectUtil.isEmpty(one)){
            return AjaxResult.error("订单不存在");
        }
        String status = "";
        switch (one.getCollectionStatus()){
            //催收状态(0-待催收 1-催收中 2-承诺还款 3-催收成功 4-催收失败)
            case 0:
                status="待催收";
                break;
            case 1:
                status="催收中";
                break;
            case 2:
                status="承诺还款";
                break;
            case 3:
                status="催收成功";
                break;
            case 4:
                status="催收失败";
                break;
        }
        return AjaxResult.success(status);
    }

}
